package cn.tedu.egweibo2.controller;

import cn.tedu.egweibo2.mapper.UserMapper;
import cn.tedu.egweibo2.pojo.dto.UserRegDTO;
import cn.tedu.egweibo2.pojo.entity.User;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;

@Slf4j
@RequestMapping("/users")
@RestController
@Api(tags = "用户模块")
public class UserController {
    @Autowired
    private UserMapper userMapper;
    @ApiOperation("用户注册")
    @PostMapping("/reg")
    public int regUser(@RequestBody UserRegDTO userRegDTO){
        log.trace("UserController:regUser()");
        log.info("开始处理用户注册");
        log.debug(userRegDTO.toString());

        if(userMapper.selectByUsername(userRegDTO.getUsername())!=null){
            return 2;
        }
        User user = new User();
        BeanUtils.copyProperties(userRegDTO,user);
        user.setCreated(new Date());
        log.debug(user.toString());
        userMapper.insert(user);
        return 1;
    }
}
